package org.example.rabbitmq.client.concurrent;

import org.example.rabbitmq.config.MultiQueueProperties;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConsumerAutoConfig {
    @Autowired
    RabbitAdmin rabbitAdmin;
    @Autowired
    LatencyChannelAwareMessageListener messageListener;
    @Autowired
    ConnectionFactory connectionFactory;
    @Autowired
    MultiQueueProperties multiQueueProperties;

    @Bean
    public DynamicMessageListenerContainerLifecycle dynamicContainerLifecycle() {
        return DynamicMessageListenerContainerBuilder.create()
                .queueSize(multiQueueProperties.getQueueSize())
                .concurrentConsumers(1)
                .withQueueNameGenerator(i -> multiQueueProperties.getQueueNameWithIndex(i))
                .withRoutingKeyGenerator(i -> multiQueueProperties.getRoutingKeyWithIndex(i))
                .initializeQueueAndBinding(rabbitAdmin, multiQueueProperties.getExchangeName())
                .initializeContainer(connectionFactory, messageListener)
                .build();
    }
}